# Makefile support for the menuconfig system

# Find all Kconfig files for all components
# COMPONENT_KCONFIGS := $(foreach components,$(COMPONENT_PATHS),$(wildcard $(components)/Kconfig))
# COMPONENT_KCONFIGS_PROJBUILD := $(foreach components,$(COMPONENT_PATHS),$(wildcard $(components)/Kconfig.projbuild))

COMPONENT_KCONFIGS := $(wildcard $(PROJECT_PATH)/components/*/Kconfig)
COMPONENT_KCONFIGS_PROJBUILD := $(PROJECT_PATH)/components/Kconfig.projbuild
COMPONENT_SDKCONFIG_RENAMES := $(PROJECT_PATH)/components/sdkconfig.rename

ifeq ($(OS),Windows_NT)
# kconfiglib requires Windows-style paths for kconfig files
COMPONENT_KCONFIGS := $(shell cygpath -m $(COMPONENT_KCONFIGS))
COMPONENT_KCONFIGS_PROJBUILD := $(shell cygpath -m $(COMPONENT_KCONFIGS_PROJBUILD))
COMPONENT_SDKCONFIG_RENAMES := $(shell cygpath -m $(COMPONENT_SDKCONFIG_RENAMES))
endif

fortest:
	@echo COMPONENT_KCONFIGS $(COMPONENT_KCONFIGS)

# For doing make menuconfig etc
KCONFIG_TOOL_DIR = $(PROJECT_PATH)/tools/kconfig

# target sdkconfig and sdkconfig.old file define
SDKCONFIG ?= $(PROJECT_PATH)/sdkconfig
SDKCONFIG_RENAME ?= $(PROJECT_PATH)/components/sdkconfig.rename
SDKCONFIG_DEFAULTS ?= $(PROJECT_PATH)/sdkconfig.defaults

KCONFIG_AUTOCONFIG ?= $(PROJECT_PATH)/obj/auto.conf
SDKCONFIG_MAKEFILE ?= $(KCONFIG_AUTOCONFIG)

# Non-interactive targets. Mostly, those for which you do not need to build a binary
NON_INTERACTIVE_TARGET += defconfig clean% %clean help list-components print_flash_cmd check_python_dependencies
MAKECMDGOALS ?= all

# Workaround to run make parallel (-j). mconf-idf and conf-idf cannot be made simultaneously
$(KCONFIG_TOOL_DIR)/mconf-idf: $(KCONFIG_TOOL_DIR)/conf-idf

# Set host compiler and binutils
HOSTCC := /usr/bin/gcc
HOSTLD := ld

# reset MAKEFLAGS as the menuconfig makefile uses implicit compile rules
$(KCONFIG_TOOL_DIR)/mconf-idf $(KCONFIG_TOOL_DIR)/conf-idf: $(wildcard $(KCONFIG_TOOL_DIR)/*.c)  $(wildcard $(KCONFIG_TOOL_DIR)/*.y)
	MAKEFLAGS="" CC=$(HOSTCC) LD=$(HOSTLD) \
	$(MAKE) -C $(KCONFIG_TOOL_DIR)

ifeq ("$(wildcard $(SDKCONFIG))","")
# if no configuration file is present we need a rule for it
ifeq ("$(filter $(NON_INTERACTIVE_TARGET), $(MAKECMDGOALS))","")
# if special non-interactive item is not a named target (eg. 'defconfig', 'clean')
# run defconfig then menuconfig to get the initial config
$(SDKCONFIG): menuconfig
menuconfig: defconfig
else
# otherwise, just run defconfig
$(SDKCONFIG): defconfig
endif # ("$(wildcard $(SDKCONFIG))","")
endif # ("$(filter $(NON_INTERACTIVE_TARGET), $(MAKECMDGOALS))","")

ifneq ("$(wildcard $(SDKCONFIG_DEFAULTS))","")
ifeq ($(OS),Windows_NT)
DEFAULTS_ARG:=--defaults $(shell cygpath -m $(SDKCONFIG_DEFAULTS))
else
DEFAULTS_ARG:=--defaults $(SDKCONFIG_DEFAULTS)
endif
else
DEFAULTS_ARG:=
endif

# macro for running confgen.py
define RunConfGen
	python $(PROJECT_PATH)/tools/kconfig_new/confgen.py \
		--kconfig $(ROOT_PATH)/Kconfig \
		--config $(SDKCONFIG) \
		--sdkconfig-rename $(SDKCONFIG_RENAME) \
		--env "COMPONENT_KCONFIGS=$(strip $(COMPONENT_KCONFIGS))" \
		--env "COMPONENT_KCONFIGS_PROJBUILD=$(strip $(COMPONENT_KCONFIGS_PROJBUILD))" \
		--env "COMPONENT_SDKCONFIG_RENAMES=$(strip $(COMPONENT_SDKCONFIG_RENAMES))" \
		--env "IDF_CMAKE=n" \
		$(DEFAULTS_ARG) \
		--output config ${SDKCONFIG} \
		--output makefile $(SDKCONFIG_MAKEFILE) \
		--output header $(PROJECT_PATH)/app/sdkconfig.h \
		$1
endef

# macro for the commands to run kconfig tools conf-idf or mconf-idf.
# $1 is the name (& args) of the conf tool to run
# Note: Currently only mconf-idf is used for compatibility with the CMake build system. The header file used is also
# the same.
define RunConf
	@echo kconfig is $(SDKCONFIG)
	cd $(PROJECT_PATH); KCONFIG_AUTOHEADER=$(abspath $(PROJECT_PATH)/app/sdkconfig.h) \
	KCONFIG_AUTOCONFIG=$(KCONFIG_AUTOCONFIG) \
	COMPONENT_KCONFIGS="$(COMPONENT_KCONFIGS)" KCONFIG_CONFIG=$(SDKCONFIG) \
	COMPONENT_KCONFIGS_PROJBUILD="$(COMPONENT_KCONFIGS_PROJBUILD)" \
	$(KCONFIG_TOOL_DIR)/$1 $(ROOT_PATH)/Kconfig
endef

menuconfig: $(KCONFIG_TOOL_DIR)/mconf-idf
#	$(summary) MENUCONFIG
#	$(call RunConfGen,--dont-write-deprecated)
	# RunConfGen before menuconfig ensures that deprecated options won't be ignored (they've got renamed)
	$(call RunConf,mconf-idf)
	# RunConfGen after menuconfig ensures that deprecated options are appended to $(SDKCONFIG) for backward compatibility
	$(call RunConfGen,)

defconfig: $(KCONFIG_TOOL_DIR)/conf-idf
#	$(summary) DEFCONFIG
ifneq ("$(wildcard $(SDKCONFIG_DEFAULTS))","")
	cat $(SDKCONFIG_DEFAULTS) >> $(SDKCONFIG)  # append defaults to sdkconfig, will override existing values
endif
	$(call RunConf,conf-idf --olddefconfig)

config-head:
	@echo $(COMPONENT_SDKCONFIG_RENAMES)
	$(call RunConfGen,)

.PHONY: config-clean defconfig menuconfig
config-clean:
	MAKEFLAGS="" $(MAKE) -C $(KCONFIG_TOOL_DIR) clean
	rm -rf $(PROJECT_PATH)/app/sdkconfig.h

